﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Guest_room.BLL;

namespace Guest_room.admin
{
    public partial class audit : Form
    {
        private GuestRoomService guestRoomService;

        public audit()
        {
            InitializeComponent();
            guestRoomService = new GuestRoomService();
        }

        public static int orderid;

        void showAll()
        {
            var orders = guestRoomService.GetAllOrders();
            dataGridView1.Rows.Clear();

            foreach (var order in orders)
            {
                int index = dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells["column1"].Value = order.订单编号.ToString();
                dataGridView1.Rows[index].Cells["column9"].Value = order.入住经办员编号.ToString();
                dataGridView1.Rows[index].Cells["column2"].Value = order.客户姓名;
                dataGridView1.Rows[index].Cells["column3"].Value = order.入住天数.ToString();
                dataGridView1.Rows[index].Cells["column4"].Value = order.客房编号.ToString();
                dataGridView1.Rows[index].Cells["column6"].Value = order.订单总价.ToString();
                dataGridView1.Rows[index].Cells["column7"].Value = order.订单日期.ToString();
                dataGridView1.Rows[index].Cells["column8"].Value = order.订单状态;
                dataGridView1.Rows[index].Cells["column10"].Value = "审核";
                dataGridView1.Rows[index].Cells["column11"].Value = "订单详情";
            }

            // 字体居中
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            // 行背景
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                if (dataGridView1.Rows[i].Cells["column8"].Value.ToString() == "未审核")
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightYellow;
                }
                else if (dataGridView1.Rows[i].Cells["column8"].Value.ToString() == "已审核")
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                }
            }
        }

        private void audit_Load(object sender, EventArgs e)
        {
            showAll();
            label1.Text = "当前用户：" + (Login.username ?? Home.username);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = dataGridView1.CurrentRow.Index;
            int cIndex = dataGridView1.CurrentCell.ColumnIndex;

            if (cIndex == 9)
            {
                var orderId = Convert.ToInt32(dataGridView1.Rows[n].Cells["column1"].Value.ToString());
                var currentStatus = dataGridView1.Rows[n].Cells["column8"].Value.ToString();

                if (currentStatus == "未审核")
                {
                    guestRoomService.UpdateOrderStatus(orderId, "已审核");
                    dataGridView1.Rows[n].Cells["column8"].Value = "已审核";
                }
                else
                {
                    DialogResult dr = MessageBox.Show("是否取消审核", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        guestRoomService.UpdateOrderStatus(orderId, "未审核");
                        dataGridView1.Rows[n].Cells["column8"].Value = "未审核";
                    }
                }

                audit_Load(sender, e);
            }

            if (cIndex == 8)
            {
                orderid = Convert.ToInt32(dataGridView1.Rows[n].Cells["column1"].Value.ToString());
                this.Dispose();
                orderdetails orderdetails = new orderdetails();
                orderdetails.ShowDialog();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
            index index = new index();
            index.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            showAll();
            // 隐藏已审核的行
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                if (dataGridView1.Rows[i].Cells["column8"].Value.ToString() == "已审核")
                {
                    dataGridView1.Rows[i].Visible = false;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            showAll();
            // 隐藏未审核的行
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                if (dataGridView1.Rows[i].Cells["column8"].Value.ToString() == "未审核")
                {
                    dataGridView1.Rows[i].Visible = false;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            showAll();
        }
    }
}
